######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figureI-plots.R
#  :: produces balance plots for figure I

rm(list=ls())
setwd('~/Dropbox/rainReplication')              

if(length(which(installed.packages()[,1]=='xtable'))!=1){
	install.packages('xtable')                        
}   

library(xtable)

source('plot.pval.R')
source('percent.bal.R')
     
#for(fes.type in 2:3){ 
#	for(non.missings in c(0,4)){
 
fes.type=3
non.missings=4
   	
load(paste('balance/figureI-',non.missings,'_',fes.type,'.Rdata',sep=''))

res=data.frame(matrix(NA,length(placebo1_fe_sumcl[-c(1),3]),9))
for(j in 1:nrow(res)){
	res[j,2]=placebo1_fe_sumcl[j+1,3]
	res[j,3]=placebo2_fe_sumcl[j+1,3]
	res[j,4]=placebo1_fe_sumcl[j+1,3]
	res[j,5]=placebo2_fe_sumcl[j+1,3]
	
	res[j,6]=placebo1_fe_sumcl[j+1,4]
	res[j,7]=placebo2_fe_sumcl[j+1,4]
	res[j,8]=placebo1_fe_sumcl[j+1,4]
	res[j,9]=placebo2_fe_sumcl[j+1,4]
	
}

res[,2:9]=round(res[,2:9],digits=3)
res[,1]=names(placebo1_fe_sumcl[-c(1),3])

res=rbind(res[1:22,],NA,res[23:nrow(res),])          

res[,1]=c(
expression("Dem. Incumbent"["t"]),
expression("District Ideology"["t-1"]),
expression("Midterm Election"["t"]),           
expression("Presidential Co-Partisan"["t"]),
expression("% Black"["t"]),
expression("% Construction"["t"]),
expression("% Education"["t"]),
expression("Median Income Per Capita"["t"]),
expression("% Farmer"["t"]),
expression("% Foreign Born"["t"]),
expression("% Govt. Worker"["t"]),
expression("% Manufacturing"["t"]),
expression("Log Population"["t"]),
expression("% Unemployed"["t"]),
expression("% Urban"["t"]),
expression("% Retail Worker"["t"]),
expression("Dem. Sec. of State"["t"]),
expression("Dem. Governor"["t"]),
expression("Competitiveness"["t"]),
expression("Redistricted"["t"]),
expression("Dem. Incumbent"["t-1"]),
expression("District Ideology"["t-2"]),  
expression("Midterm Election"["t-1"]),          
expression("Presidential Co-Partisan"["t-1"]),
expression("% Black"["t-1"]),
expression("% Construction"["t-1"]),
expression("% Education"["t-1"]),
expression("Median Income Per Capita"["t-1"]),
expression("% Farmer"["t-1"]),
expression("% Foreign Born"["t-1"]),
expression("% Govt. Worker"["t-1"]),
expression("% Manufacturing"["t-1"]),
expression("Log Population"["t-1"]),
expression("% Unemployed"["t-1"]),
expression("% Urban"["t-1"]),
expression("% Retail Worker"["t-1"]),
expression("Dem. Sec. of State"["t-1"]),
expression("Dem. Governor"["t-1"]),
expression("Redistricted"["t-1"]),
expression("Competitiveness"["t-1"]),
expression("Dem. Incumbent"["t-2"]),
expression("District Ideology"["t-3"]),  
expression("Midterm Election"["t-2"]),      
expression("Presidential Co-Partisan"["t-2"]),
expression("% Black"["t-2"]),
expression("% Construction"["t-2"]),
expression("% Education"["t-2"]),
expression("Median Income Per Capita"["t-2"]),
expression("% Farmer"["t-2"]),
expression("% Foreign Born"["t-2"]),
expression("% Govt. Worker"["t-2"]),
expression("% Manufacturing"["t-2"]),
expression("Log Population"["t-2"]),
expression("% Unemployed"["t-2"]),
expression("% Urban"["t-2"]),
expression("% Retail Worker"["t-2"]),
expression("Dem. Sec. of State"["t-2"]),
expression("Dem. Governor"["t-2"]),
expression("Redistricted"["t-2"]),
expression("Competitiveness"["t-2"]),
expression("Dem. Vote Margin"["t-1"]),
expression("Dem. Vote Margin"["t-2"]),
expression("Inc. Ideal Point"["t-1"]),
expression("Inc. Ideal Point"["t-2"]),
expression("(Undeviated) Election Rain"["t-1"]),
expression("(Deviated) Election Rain"["t-1"]))
 
#res=res[c(1:37,39,38,40:57,59,58,60:66),]
res=res[c(1:38,40,39,41:58,60,59,61:66),]

#x=c(59:64,1,2,19,20,17,18,3,4,8,13,5,6,7,9,10,11,12,14,15,16,
#c(1,2,19-1,20-1,17-1,18-1,4-1,8-1,13-1,5-1,6-1,7-1,9-1,10-1,11-1,12-1,14-1,15-1,16-1)+20,
#c(1,2,19-1,20-1,17-1,18-1,4-1,8-1,13-1,5-1,6-1,7-1,9-1,10-1,11-1,12-1,14-1,15-1,16-1)+39)
          
x=c(61:66,
  1,2,19,20,17,18,3,4,8,13,5,6,7,9,10,11,12,14,15,16,
c(1,2,19,20,17,18,3,4,8,13,5,6,7,9,10,11,12,14,15,16)+20,
c(1,2,19,20,17,18,3,4,8,13,5,6,7,9,10,11,12,14,15,16)+40) 


res=res[x,]
 
x=c(1:6,
7,7+20,7+40,
8,8+20,8+40,
9,9+20,9+40,
10,10+20,10+40,
11,11+20,11+40,
12,12+20,12+40,
13,13+20,13+40,
14,14+20,14+40,
15,15+20,15+40,
16,16+20,16+40,
17,17+20,17+40,
18,18+20,18+40,
19,19+20,19+40,
20,20+20,20+40,
21,21+20,21+40,
22,22+20,22+40,
23,23+20,23+40,
24,24+20,24+40,
25,25+20,25+40,
26,26+20,26+40)

res=res[x,]
             
res=res[-c(1:4),]
#res=res[order(abs(rowMeans(cbind(res[,2],res[,3])))),]

pdf(paste("balance/rainBalance-",non.missings,"_",fes.type,".pdf",sep=''),width=6,height=10)#,width=3000,height=5000)#,res=500)
plot.pval(results=res[,c(1,2:3,6:7)], title="", legend=TRUE,legendx=0.75,legendy=16.8,#legendx=0.7,legendy=7.8, 
	textsize=0.65, parcex=.7, at1=-0.35, at2=-0.15, at3=-0.9,xlim1=-0.85)
dev.off()   
#	}
#}

